package com.castle.fortress.admin.study.exam.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.study.exam.dto.StudyQuestionbankDto;
import com.castle.fortress.admin.study.exam.entity.StudyQuestionbankEntity;
import com.castle.fortress.admin.study.exam.mapper.StudyQuestionMapper;
import com.castle.fortress.admin.study.exam.mapper.StudyQuestionbankMapper;
import com.castle.fortress.admin.study.exam.service.StudyQuestionbankService;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.enums.YesNoEnum;
import com.castle.fortress.common.utils.CommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 题库管理 服务实现类
 *
 * @author castle
 * @since 2023-02-09
 */
@Service
public class StudyQuestionbankServiceImpl extends ServiceImpl<StudyQuestionbankMapper, StudyQuestionbankEntity> implements StudyQuestionbankService {
    @Autowired
    private StudyQuestionMapper studyQuestionMapper;

    /**
    * 获取查询条件
    * @param studyQuestionbankDto
    * @return
    */
    public QueryWrapper<StudyQuestionbankEntity> getWrapper(StudyQuestionbankDto studyQuestionbankDto){
        QueryWrapper<StudyQuestionbankEntity> wrapper= new QueryWrapper();
        if(studyQuestionbankDto != null){
            StudyQuestionbankEntity studyQuestionbankEntity = ConvertUtil.transformObj(studyQuestionbankDto,StudyQuestionbankEntity.class);
            wrapper.eq(studyQuestionbankEntity.getQbtypeId() != null,"qbtype_id",studyQuestionbankEntity.getQbtypeId());
            wrapper.like(StrUtil.isNotEmpty(studyQuestionbankEntity.getQbName()),"qb_name",studyQuestionbankEntity.getQbName());
            wrapper.eq(studyQuestionbankEntity.getStatus() != null,"status",studyQuestionbankEntity.getStatus());
        }
        wrapper.orderByDesc("create_time");
        return wrapper;
    }


    @Override
    public IPage<StudyQuestionbankDto> pageStudyQuestionbank(Page<StudyQuestionbankDto> page, StudyQuestionbankDto studyQuestionbankDto) {
        QueryWrapper<StudyQuestionbankEntity> wrapper = getWrapper(studyQuestionbankDto);
        Page<StudyQuestionbankEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<StudyQuestionbankEntity> studyQuestionbankPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<StudyQuestionbankDto> pageDto = new Page(studyQuestionbankPage.getCurrent(), studyQuestionbankPage.getSize(),studyQuestionbankPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(studyQuestionbankPage.getRecords(),StudyQuestionbankDto.class));
        return pageDto;
    }

    @Override
    public IPage<StudyQuestionbankDto> pageStudyQuestionbankExtends(Page<StudyQuestionbankDto> page, StudyQuestionbankDto studyQuestionbankDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        StudyQuestionbankEntity studyQuestionbankEntity = ConvertUtil.transformObj(studyQuestionbankDto,StudyQuestionbankEntity.class);
        List<StudyQuestionbankEntity> studyQuestionbankList=baseMapper.extendsList(pageMap,studyQuestionbankEntity);
        //初始化开放状态
        for(StudyQuestionbankEntity entity:studyQuestionbankList){
            if(CommonUtil.verifyParamNull(entity.getOpenStartTime(),entity.getOpenEndTime()) || entity.getOpenStartTime().compareTo(new Date()) == 1 || entity.getOpenEndTime().compareTo(new Date()) == -1){
                entity.setStatus(YesNoEnum.NO.getCode());
            }else{
                entity.setStatus(YesNoEnum.YES.getCode());
            }
        }
        Long total = baseMapper.extendsCount(studyQuestionbankEntity);
        Page<StudyQuestionbankDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(studyQuestionbankList,StudyQuestionbankDto.class));
        return pageDto;
    }
    @Override
    public StudyQuestionbankDto getByIdExtends(Long id){
        StudyQuestionbankEntity  studyQuestionbankEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(studyQuestionbankEntity,StudyQuestionbankDto.class);
    }

    @Override
    public List<StudyQuestionbankDto> listStudyQuestionbank(StudyQuestionbankDto studyQuestionbankDto){
        QueryWrapper<StudyQuestionbankEntity> wrapper = getWrapper(studyQuestionbankDto);
        List<StudyQuestionbankEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,StudyQuestionbankDto.class);
    }

    @Override
    public IPage<StudyQuestionbankDto> pageOpenStudyQuestionbank(Page<StudyQuestionbankDto> page, StudyQuestionbankDto studyQuestionbankDto) {
        QueryWrapper<StudyQuestionbankEntity> wrapper = new QueryWrapper<>();
        wrapper.le("open_start_time",new Date());
        wrapper.ge("open_end_time",new Date());
        wrapper.inSql("id","select  distinct  qb_id from study_question");
        wrapper.orderByAsc("sort");
        Page<StudyQuestionbankEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<StudyQuestionbankEntity> studyQuestionbankPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<StudyQuestionbankDto> pageDto = new Page(studyQuestionbankPage.getCurrent(), studyQuestionbankPage.getSize(),studyQuestionbankPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(studyQuestionbankPage.getRecords(),StudyQuestionbankDto.class));
        return pageDto;
    }

    @Override
    public void delByTypeId(Long typeId) {
        //删除题库下的题目
        studyQuestionMapper.delByQuestionTypeId(typeId);
        //删除分类下的题库
        QueryWrapper<StudyQuestionbankEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("qbtype_id",typeId);
        baseMapper.delete(wrapper);
    }

    @Override
    public boolean checkName(StudyQuestionbankEntity studyQuestionbankEntity) {
        QueryWrapper<StudyQuestionbankEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("qb_name",studyQuestionbankEntity.getQbName());
        wrapper.eq("qbtype_id",studyQuestionbankEntity.getQbtypeId());
        wrapper.ne(studyQuestionbankEntity.getId()!=null,"id",studyQuestionbankEntity.getId());
        List<StudyQuestionbankEntity> list = baseMapper.selectList(wrapper);
        return (list!=null&&list.size()>0)?true:false;
    }
}

